package com.myhome.money.base.javaio;

import java.io.*;

/**
 *
 */
public class BytesStream {

    public static void main(String[] args) throws IOException {
        writeByteToFile();
        readByteByFile();
    }

    //使用字节流写文件
    public static void writeByteToFile() throws IOException {
        String str = "hello world";
        byte[] bytes = str.getBytes();
        File file = new File("D:/javaio/io/test.txt");
        //因为是用字节流来写媒介，所以对应的是OutputStream
        //又因为媒介对象是文件，所以用到子类是FileOutputStream
        OutputStream outputStream = new FileOutputStream(file);
        //outputStream.write(bytes,10,12);
        outputStream.write(bytes);
        outputStream.close();
    }

    public static void readByteByFile() throws IOException{
        File file = new File("D:/javaio/io/test.txt");
        InputStream inputStream=new FileInputStream(file);
        byte[] bytes = new byte[(int)file.length()];
        inputStream.read(bytes);
        String str = new String(bytes);
        System.out.printf("读取结果：" + str);
        inputStream.close();
    }

}
